package com.supply.dl.domain;

import com.supply.dl.utils.utils.JsonParseInterface;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Create by liao on 15/11/28
 */
public class BaseResponse implements JsonParseInterface
{
	boolean r_code;
	String r_msg;
	String r_data;
	long r_ts;

	public String getR_data()
	{
		return r_data;
	}

	protected static Integer optInt(JSONObject json, String key)
	{
		return optInt(json, key, null);
	}

	protected static Integer optInt(JSONObject json, String key, Integer def)
	{
		if (json.has(key))
		{
			int r = json.optInt(key, Integer.MAX_VALUE);
			if (r != Integer.MAX_VALUE)
				return r;
		}
		return def;
	}

	protected static boolean optBool(JSONObject json, String key, boolean def)
	{
		if (json.has(key))
		{
			String s = json.optString(key, null);
			if (s != null && (s.equals("1") || s.equalsIgnoreCase("true")))
				return true;
			return false;
		}
		return def;
	}

	protected static String optString(JSONObject json, String key)
	{
		return optString(json, key, null);
	}

	protected static String optLong(JSONObject json, String key)
	{
		return optLong(json, key);
	}

	protected static long optLong(JSONObject json, String key, long def)
	{
		if (json.has(key))
		{
			return json.optLong(key, def);
		}
		return 0;
	}

	protected static String optString(JSONObject json, String key, String def)
	{
		if (json.has(key))
		{
			return json.optString(key, def);
		}
		return null;
	}

	@Override public JSONObject buildJson() throws JSONException
	{
		return null;

	}

	public JSONObject dataJson;

	@Override public void parseJson(JSONObject json)
	{
		r_code = optBool(json, "handleState", false);
		r_msg = json.optString("message", null);
		dataJson = json.optJSONObject("data");
	}

	@Override public String toString()
	{
		return "Response{" + "code=" + r_code + ", msg='" + r_msg + '\'' + ", r_ts=" + r_ts + '}';
	}

	@Override public String getShortName()
	{
		return null;
	}

	public boolean isSuccess()
	{
		return r_code;
	}

	public String getMsg()
	{
		return this.r_msg;
	}
}
